package com.pickgliss.utils
{
	import com.pickgliss.geom.InnerRectangle;
	import com.pickgliss.toplevel.StageReferance;
	import com.pickgliss.ui.core.Component;
	import com.pickgliss.ui.image.Image;
	import com.pickgliss.ui.text.FilterFrameText;
	
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.InteractiveObject;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	
	import spark.primitives.Rect;

	/**
	 * 
	 * @author Herman
	 * 
	 */	
	public final class DisplayUtils
	{
		
		/**
		 * 
		 * @param $width 矩形的宽
		 * @param $height 矩形的高
		 * @param target : 绘制矩形的shape
		 * @return 创建完成的shape
		 * 
		 */		
		public static function drawRectShape($width:Number,$height:Number,target:Shape = null):Shape
		{
			var sp:Shape;
			if(target == null)
			{
				sp = new Shape();
			}else
			{
				sp = target;
			}
			sp.graphics.clear();
			sp.graphics.beginFill(0xFF0000,1);
			sp.graphics.drawRect(0,0,$width,$height);
			sp.graphics.endFill();
			return sp;
		}
		/**
		 * 绘制文本的shape
		 * @param 目标文本框
		 * @return 文本的shape
		 * 
		 */		
		public static function drawTextShape(sourceTextField:TextField):DisplayObject
		{
			var textBitmapData:BitmapData = new BitmapData(sourceTextField.width,sourceTextField.height,true,0xff0000);
			textBitmapData.draw(sourceTextField);
			var textGraphics:Shape=new Shape();
			textGraphics.cacheAsBitmap = true;
			for (var i:uint=0; i<textBitmapData.width; i++) {
				for (var j:uint=0; j<textBitmapData.height; j++) {
					var col:uint=textBitmapData.getPixel32(i,j);
					var alphaChannel:uint = col >> 24 & 0xFF;
					var alphaValue:Number = alphaChannel/0xFF;
					if (col > 0) {
						textGraphics.graphics.beginFill(0x000000,alphaValue);
						textGraphics.graphics.drawCircle(i,j,1);
					}
				}
			}
			return textGraphics;
		}
		/**
		 * 检查点是否在显示范围之内
		 * @param point : 需要检查的点
		 * @param parent : 此点所参考的父级，如果为stage可以不传
		 * @return point是否在显示范围之内
		 * 
		 */		
		public static function isInTheStage(point:Point,parent:DisplayObjectContainer = null):Boolean
		{
			var stagePoint:Point = point;
			if(parent)
			{
				stagePoint = parent.localToGlobal(point);
			}
			if(stagePoint.x < 0 || stagePoint.y < 0 || stagePoint.x > StageReferance.stageWidth || stagePoint.y > StageReferance.stageHeight) 
			{
				return false;
			}
			return true;
		}
		
		public static function layoutDisplayWithInnerRect(coms:DisplayObject,innerRect:InnerRectangle,width:int,height:int):void
		{
			if(innerRect == null)return;
			if(coms is Component)Component(coms).beginChanges();
			var rect:Rectangle = innerRect.getInnerRect(width,height);
			coms.x = rect.x;
			coms.y = rect.y;
			coms.width = rect.width;
			coms.height = rect.height;
			if(coms is Component)Component(coms).commitChanges();
		}	
		/**
		 * 缩放可视化对象以使其充满矩形区域
		 * @param obj 要进行缩放的可视化对象.
		 * @param rect 要填充满的矩形.
		 * @param zoomOut 是否进行缩小
		 * @return 
		 * 
		 */		
		public static function fullInRect(obj : DisplayObject, rect : Rectangle, zoomOut : Boolean = true):Rectangle
		{
			var rltRect : Rectangle = new Rectangle();
			var scale : Number;
			if(obj.width/rect.width > obj.height/rect.height){
				scale = rect.height/obj.height;
			}else{
				scale = rect.width/obj.width;
			}
			if(!zoomOut && scale<1){
				rltRect = new Rectangle(rect.x, rect.y, obj.width, obj.height);				
			}else{
				rltRect = new Rectangle(rect.x, rect.y, obj.width*scale, obj.height*scale);
			}
			
			obj.x = rltRect.x;
			obj.y = rltRect.y;
			obj.width = rltRect.width;
			obj.height = rltRect.height;
			
			return rltRect;
		}
		
		public static function setFrame(display:DisplayObject,frameIndex:int):void
		{
			if(display is Image)
			{
				Image(display).setFrame(frameIndex);
			}else if(display is MovieClip)
			{
				MovieClip(display).gotoAndStop(frameIndex);
			}else if(display is FilterFrameText)
			{
				FilterFrameText(display).setFrame(frameIndex);
			}
			return;
		}
		
		public static function setDisplayObjectNotEnable(display:DisplayObject):void
		{
			if(display is InteractiveObject)
			{
				InteractiveObject(display).mouseEnabled = false;
			}
			if(display is DisplayObjectContainer)
			{
				DisplayObjectContainer(display).mouseChildren = false;
				DisplayObjectContainer(display).mouseEnabled = false;
			}
		}
		
		public static function getTextFieldLineHeight(field:TextField):int
		{
			return field.getLineMetrics(0).height;
		}
		
		public static function getTextFieldCareLinePosY(field:TextField):Number
		{
			var lastCareCharIndex:int = field.caretIndex-1;
			var charCode:int = field.text.charCodeAt(lastCareCharIndex);
			var lastCareCharlineIndex:int = field.getLineIndexOfChar(lastCareCharIndex);
			var careCharLineIndex:int = 0;
			if(charCode == 13)
			{
				careCharLineIndex = lastCareCharlineIndex+1;
			}else
			{
				careCharLineIndex = lastCareCharlineIndex;
			}
			return getTextFieldLineHeight(field)*careCharLineIndex;
		}
		
		public static function getTextFieldCareLinePosX(field:TextField):Number
		{
			var lastCareCharIndex:int = field.caretIndex-1;
			var lastCharPos:Rectangle = field.getCharBoundaries(lastCareCharIndex);
			if(lastCharPos == null)return 0;
			return lastCharPos.x+lastCharPos.width;
		}
		
		public static function getVisibleSize (o:DisplayObject):Rectangle 
		{
			var bitmapDataSize:int = 2000;
			var bounds:Rectangle;
			var bitmapData:BitmapData = new BitmapData(bitmapDataSize, 
				bitmapDataSize,
				true,
				0);
			bitmapData.draw(o);
			bounds = bitmapData.getColorBoundsRect( 0xFF000000, 0x00000000, false );
			bitmapData.dispose(); 
			var retultRect:Rectangle = new Rectangle(bounds.x,bounds.y,bounds.x + bounds.width,bounds.y + bounds.height);
			return retultRect;
		}
		
		public static function getTextFieldMaxLineWidth(value:String,format:TextFormat,isHtmlText:Boolean):Number
		{
			var textField:TextField = new TextField();
			textField.autoSize = TextFieldAutoSize.LEFT;
			var linesText:Array
			if(isHtmlText)
			{
				value = value.replace("<BR>","\n");
				value = value.replace("<Br>","\n");
				value = value.replace("<bR>","\n");
				value = value.replace("<br>","\n");
			}
			linesText = value.split("\n");
			var maxWidth:Number = 0;
			for (var i:int = 0; i<linesText.length; i++)
			{
				if(isHtmlText)
				{
					textField.htmlText = linesText[i];
				}else
				{
					textField.text = linesText[i];
					textField.setTextFormat(format);
				}
				maxWidth = Math.max(maxWidth,textField.width);
			}
			return maxWidth+2;
		}
		
		public static function isTargetOrContain(target:DisplayObject,container:DisplayObject):Boolean
		{
			if(target == null)return false;
			if (target == container)
			{
				return true;
			}
			if (container is DisplayObjectContainer)
			{
				return DisplayObjectContainer(container).contains(target);
			}
			return false;
		}
		
		public static function getPointFromObject(pt:Point,pointInDisplay:DisplayObject,targetDisplay:DisplayObject):Point
		{
			var pt:Point = pointInDisplay.localToGlobal(pt);
			var targetLocalPoint:Point = targetDisplay.globalToLocal(pt);
			return targetLocalPoint;
		}
		
		public static function getHitArea(bmd:BitmapData, threshold:uint = 2):Sprite
		{
			var sp:Sprite = new Sprite();
			
			sp.graphics.beginFill(0,0);
			for(var _x:uint = 0; _x < bmd.width; _x += threshold)
			{
				for(var _y:uint = 0; _y < bmd.height; _y += threshold)
				{
					//					trace(bmd.getPixel32(_x, _y) + " _x: " + _x + " _y: " + _y);
					if(bmd.getPixel32(_x, _y))
					{
						sp.graphics.drawRect(_x, _y, threshold, threshold);
					}
				}
			}
			sp.graphics.endFill();
			
			return sp;
		}
		public function DisplayUtils()
		{
		}
	}
}